<?php
$dbname = 'tcntrans_main';
$dbhost = 'localhost';
$dbuser = 'tcntrans';
$dbpass = 'hJV65VUX';
$db = mysql_connect($dbhost,$dbuser,$dbpass);

function getAdministrators() {
	global $userLevelArray, $stringJSON, $dbname, $db, $db_selected;
	require_once("../classes/JSON.php");
	
	$sql = "SELECT * from transLogin WHERE Disabled = 0 ORDER BY FullName ASC";
	$result = mysql_db_query($dbname,$sql);
	if (mysql_num_rows($result)>0) {
		$stringJSON = '({"Administrators":[';
		while ($value = mysql_fetch_assoc($result)) {
			$newArray = array(
							'UID'        => $value[UID],
							'Delete'     => '<a href="#" onClick="confirmDelete(\''.$value[UID].'\',\'user\'); return false;" title="Delete"><img src="images/buttonDeleteBig.gif" style="border: none;" alt="Delete" /></a>',
							'Userid'     => $value[Userid],
							'Password'   => $value[Password],
							'AdminLevel' => $value[AdminLevel],
							'FullName'   => $value[FullName],
							'Email'      => $value[Email]
							);
			
			$json = new Services_JSON();
			$stringJSON .= $json->encode($newArray).',';
		}	
		// Strip last ,
		$strLen = strlen($stringeJSON);
		$stringJSON = substr($stringJSON,0,($strLen-1));
		$stringJSON .= ']})';
	} else {
		$stringJSON = 'There are no administrators yet.';
	}
	
	return $stringJSON;
}

function deleteUser($uID) {
	// Don't completely delete user info, just set Disabled to 1
	// and remove userid and password
	global $dbname, $db, $db_selected;

	$sql = "UPDATE transLogin SET 
				Userid = '', 
				Password = '', 
				Disabled = 1 
			WHERE UID = '$uID'";
	$result = mysql_db_query($dbname,$sql);
	return $uID;
}

function editUser($phpString) {
	global $dbname, $db, $db_selected;
	
	// Parse phpString
	$phpArray = explode('|',$phpString);
	$uID       = $phpArray[0];
	$value     = fieldToDB($phpArray[1]);
	$dbField   = $phpArray[2];
	$gridRow   = $phpArray[3];
	$origValue = $phpArray[4];

	if (!empty($uID)) {
		$sql = "UPDATE transLogin SET $dbField = '$value' WHERE UID = '$uID'";
		$result = mysql_db_query($dbname,$sql);
		return $sql;
	}
}	

function saveUser($phpString) {
	global $dbname, $db, $db_selected;
	
	// Parse phpString
	$phpArray = explode('|',$phpString);
	
	$fullName   = $phpArray[0];
	$email      = $phpArray[1];
	$userid     = $phpArray[2];
	$password   = $phpArray[3];
	$adminLevel = $phpArray[4];
	
	$sql = "INSERT into transLogin (
			Userid,
			Password,
			AdminLevel,
			FullName,
			Email,
			Disabled
			) values (
			'$userid',
			'$password',
			'$adminLevel',
			'$fullName',
			'$email',
			'0'
			)";
	$result = mysql_db_query($dbname,$sql);
}

if ($_GET[getUsers]==1) {
	$stringJSON = getAdministrators();
	echo $stringJSON;
}

?>